package com.bybops.presenter

import baseproject.com.mybaseproject.mvp.contract.NewDeviceInitFragmentContract
import baseproject.com.mybaseproject.mvp.presenter.BasePresenterImpl
import baseproject.com.mybaseproject.net.ApiErrorModel
import baseproject.com.mybaseproject.net.ResultObserver
import baseproject.com.mybaseproject.net.RetrofitManager
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.app.MyApplication
import com.bybops.model.bean.LockDetail
import com.bybops.net.BaseResponse
import com.bybops.utils.BlueToothUtils

class NewDeviceInitFragmentPresenterImpl : BasePresenterImpl<NewDeviceInitFragmentContract.View>(), NewDeviceInitFragmentContract.Presenter {

    override fun activeDevice(params : HashMap<String , String?>) {
        mView?.showProgress("正在加载...")
        addDisposable(RetrofitManager.apiService.activeDevice(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<LockDetail>() {
                    override fun success(data: BaseResponse<LockDetail>) {
                        mView?.hideProgress()
                        mView?.onActiveDeviceSuccess(data.data)
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        mView?.hideProgress()
                        mView?.showToast(apiErrorModel.message)
                    }
                }))
    }

    /**
     * 依据barCode获取详情
     */
    override fun getDetailByBarCode() {
        var params = HashMap<String, String?>()
        params["access_token"] = MyApplication.instance?.mAccessToken
        params["barCode"] = mView?.getDviceId()
        mView?.showProgress("正在加载...")
        addDisposable(RetrofitManager.apiService.getDetailByBarCode(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<LockDetail>() {
                    override fun success(data: BaseResponse<LockDetail>) {
                        mView?.hideProgress()
                        mView?.showProgress("正在获取蓝牙信息...")
                        mView?.getDetailByBarCodeSuccess(data.data)
                        //获取设备详情成功  连接蓝牙并开锁
                        BlueToothUtils.readLockStatus(data.data.lockName, object : BlueToothUtils.LockStatusInf {
                            override fun openLock(isOpenSuccess: Boolean) {

                            }

                            override fun connectStatus(connected: Boolean) {
                            }

                            override fun openFail(msg: String?) {
                                mView?.hideProgress()
                                mView?.showToast(msg!!)
                            }

                            override fun currVoltageAndStatus(voltage: String?, lockStatus: Boolean) {
                                mView?.hideProgress()
                                mView?.setLockVoltage(voltage)
                            }
                        })
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        mView?.hideProgress()
                        mView?.showToast(apiErrorModel.message)
                    }
                }))
    }
}